import 'package:flutter/material.dart';
import 'Cart.dart';
import 'Home.dart';
import 'Category.dart';
import 'User.dart';
import 'package:jd_shopping/services/ScreenAdaper.dart';


class Tabs extends StatefulWidget {
  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  List<Widget> pageList = [Home(), Category(), Cart(), User()];
  var _pageController;
  int _currentIndex = 0;
  @override
  void initState() {
//页面控制器初始化
    this._pageController = new PageController(initialPage: _currentIndex);
    super.initState();
  }

  Widget _bottomNavigationBar() {
    return Container(
      child: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.red,
        items: [
          BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
            ),
            title: Text('首页'),
          ),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.category,
              ),
              title: Text(
                '分类',
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.shopping_cart,
              ),
              title: Text(
                '购物车',
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.people,
              ),
              title: Text(
                '我的',
              )),
        ],
        currentIndex: _currentIndex,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
            _pageController.jumpToPage(this._currentIndex);
          });
        },
      ),
    );
  }
  @override
  Widget build(BuildContext context) {
    ScreenAdaper.init(context);
    return Scaffold(
        bottomNavigationBar: _bottomNavigationBar(),
        body: PageView(
          physics: NeverScrollableScrollPhysics(),
          controller: _pageController,
          children: this.pageList,
          onPageChanged: (index) {
            _currentIndex = index;
          },
        ));
  }
}
